<?php
class Cert extends AdminAction{
	function index(){
		$db=$this->getDB();
		
		$certs = $db->query_assoc("select id,title,location,date_format(createtime,'%Y-%c-%d') createtime  from cert where lang = '".$this->lang."' order by createtime desc");
		$this->data['certs'] = $certs;
		
		template('cert',$this->data);
	}
	
	function deletecert(){
		$db=$this->getDB();
		$certids = P('certids');
		
		$certs = $db->query_assoc("select * from cert where id in ($certids)");
		
		$file = $this->loadLib('file');
		foreach ($certs as $ce){
			$file->unlink($ce['location']);
		}
		
		$db->query("delete from cert where id in ($certids)");
		
		message('证书删除成功！', '/admin.php/cert/index');
	}
	
	function addcert(){
		$db=$this->getDB();
		$title = P('title');
		$file = $this->loadLib('file');
		$filename = $file->uploadfile('certimg','attach/cert');
		$db->query("insert into cert (title,location,createtime,lang) values ('$title','$filename',now(),'$this->lang')");
		$message = array();
		$message = setMessage(true,'添加成功！',$message);
		echo json_encode($message);
	}
	
	function updatecert(){
		$db=$this->getDB();
		$updateid = P('updateid');
		$update_title = P('update_title');
		
		$cert = $db->get_assoc("select * from cert where id = $updateid");
		$oldfile = $cert['location'];
		
		$file = $this->loadLib('file');
		$filename = $file->uploadfile('update_certimg','attach/cert');
		$file->unlink($oldfile);
		
		$db->query("update cert set title = '$update_title',location='$filename' where id = $updateid");
		
		$message = array();
		$message = setMessage(true,'修改成功！',$message);
		echo json_encode($message);
	}
	
	/*
	function getlist(){
		$db=$this->getDB();
		$page = P('page');
		$rows = P('rows');
		$result = array();
		$t = $db->get_assoc("select count(*) total from cert ");
		$result['total'] = $t['total'];
		if(empty($page)||empty($rows)){
			$r = $db->query_assoc("select id,title,location,date_format(createtime,'%Y-%c-%d') createtime from cert where lang = '".$this->lang."' order by createtime desc limit 0,10");
			$result['rows'] = $r;
		}else{
			$start = ($page-1)*$rows;
			$r = $db->query_assoc("select id,title,location,date_format(createtime,'%Y-%c-%d') createtime from cert where lang = '".$this->lang."' order by createtime desc limit $start,$rows ");
			$result['rows'] = $r;
		}
		echo json_encode($result);
	}*/
	
}